package com.mbbmap.map;

import java.util.ArrayList;
import java.util.TreeMap;

import com.classes.Casemast;
import com.classes.CasemastFactory;
import com.classes.Notification;

public class Autoclose {

	public static void main(String[] args) {
		Autoclose fw = new Autoclose();
		fw.startWork();
	}
	
	public void startWork(){
		
		ArrayList<Casemast> cArr = CasemastFactory.getUnattendedCasemasts();
		if(cArr == null){
			System.out.println(CasemastFactory.getMessage());
			return;
		}
		
		
		try {
			for(Casemast c : cArr) {
				System.out.println("Application Reference #" + c.getReferenceNo());
				
				c.loadHistories(1);
				
				ArrayList<TreeMap<String, String>> histLogs = c.getHistories();
				
				boolean toContinue = true;
				for(TreeMap<String, String> h : histLogs) {
					if(h.get("action").equals("MID Setup Done - Request Verification")){
						toContinue = false;
					}
				}
				
				if(toContinue == false) {
					continue;
				}
				
				TreeMap<String, String> post = new TreeMap<String, String>();
				post.put("note", "*** Automatically closed after 90 days.");
				post.put("action", "Auto CLOSE In 90 Days");
				post.put("assign_to", "[uu:" + c.getCreatedBy() + "]");
				
				long hist_id = CasemastFactory.action_submit((int) c.getId(), post, "system", 1);
				if(hist_id < 0){
					System.out.println(CasemastFactory.getMessage());
					return;
				}
				
				
				Notification n = new Notification();
				n.setUser(c.getCreatedBy());
				n.setInfo("note", post.get("note"));
				n.setInfo("action", post.get("action"));
				n.setInfo("hist_id", String.valueOf(hist_id));
				n.setCasemast(c);
				
				boolean ok = n.run("email", "autoclose");
				if(ok == false){
					System.out.println(n.getMessage());
					return;
				}
			}
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

}
